﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using Gallio.Framework;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;
using System;

namespace DotNetNuke.Tests.UI.WatiN.BuildVerification
{
    [TestFixture]
    [DependsOn(typeof(InstallTests))]
    [DependsOn(typeof(UpgradeTests))]
    [DependsOn(typeof(LoginTests))]
    public class LanguageTests : BVTBase
    {
      

        #region Test Setup and TearDown

        public bool LanguagePackInstalled = false;
        public bool skip = false;

        [SetUp]
        public void SetUp()
        {
            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            if (LanguagePackInstalled && !skip)
            {
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                LoginPage loginPage = new LoginPage(homePage);
                if (!homePage.RegisterLink.Text.Contains(TestUsers.Host.DisplayName))
                {
                    if (!homePage.RegisterLink.Text.Equals("Register"))
                    {
                        loginPage.LogoffUser();

                    }
                    loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                }
                //Uninstall the language pack and delete the files
                System.Threading.Thread.Sleep(1500);
                ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
                ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
                //extensionsPage.ExtensionPackageSelectList.Select("Core Language Pack");
                //System.Threading.Thread.Sleep(1500);
                //extensionsPage.CoreLanguagePackSectionLink.Click();
                System.Threading.Thread.Sleep(2000);
                extensionsPage.GetExtensionUninstallButton(LocaleName, extensionsPage.CoreLanguagePacksTable).Click();
                System.Threading.Thread.Sleep(2000);
                extensionsPage.DeleteExtensionFilesCheckbox.Checked = true;
                extensionsPage.UninstallPackageLink.ClickNoWait();
                //ConfirmDialogHandler dialog = new ConfirmDialogHandler();
                //using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
                //{
                //    extensionsPage.UninstallPackageLink.ClickNoWait();
                //    dialog.WaitUntilExists();
                //    dialog.OKButton.Click();
                //}
                ieInstance.WaitForComplete();
                LanguagePackInstalled = false;
            }
            TearDownTest();
            skip = false;
        }

        #endregion

        #region Tests

        [Test]
        [CsvData(ResourcePath = "TestData.UpgradeList.csv", HasHeader = true)]
        [CsvData(ResourcePath = "TestData.InstallList.csv", HasHeader = true)]
        public void Add_New_Language_Pack_Via_Host_Extensions(string version, string type)
        {
            if (type.Equals("UpgradeOnly"))
            {
                TestLog.WriteLine("This test has not been run on an installation site.");
                skip = true;
                Assert.IsTrue(true);
            }
            else if (type.Equals("InstallOnly"))
            {
                TestLog.WriteLine("This test has not been run on an upgraded site.");
                skip = true;
                Assert.IsTrue(true);
            }
            else
            {
                //Arrange
                string capturePath = CapturePath + @"\LanguageTests\AddNewLanguagePackViaHostExtensions\";
                WatiNAssert watinAssert = new WatiNAssert(ieInstance, capturePath);
                if (!Directory.Exists(capturePath))
                {
                    Directory.CreateDirectory(capturePath);
                }
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                LoginPage loginPage = new LoginPage(homePage);
                try
                {
                    System.Threading.Thread.Sleep(1500);
                    loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                    ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
                    if (homePage.PageContainsErrors())
                    {
                        //Take a screen Capture
                        ieInstance.CaptureWebPageToFile(CapturePath + "/ErrorOnExtensionsPage.jpg");
                        TestLog.WriteHighlighted("WARNING:");
                        TestLog.WriteLine("An error occurred on the Extensions Page, view the corresponding screen shot and exception text log for more information on the error. ");

                        //Record any recent exceptions in the log
                        ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
                        HostSQLPage SQLPage = new HostSQLPage(homePage);
                        SQLPage.CheckForRecentExceptionsWriteToFile(capturePath + "AdminExtensionsExceptions.txt", SiteType.Equals("Qualifier"));

                        //Continue the test
                        ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
                    }
                    ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
                    LanguagePage languagePage = new LanguagePage(homePage);

                    //Act 
                    extensionsPage.InstallExtension(LanguagePath);
                    LanguagePackInstalled = true;
                    System.Threading.Thread.Sleep(3500);
                    ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
                    if (homePage.PageContainsErrors())
                    {
                        //Take a screen Capture
                        ieInstance.CaptureWebPageToFile(capturePath + "/ErrorOnLanguagesPage.jpg");
                        TestLog.WriteHighlighted("WARNING:");
                        TestLog.WriteLine("An error occurred on the Languages Page, view the corresponding screen shot and exception text log for more information on the error. ");

                        //Record any recent exceptions in the log
                        ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
                        HostSQLPage SQLPage = new HostSQLPage(homePage);
                        SQLPage.CheckForRecentExceptionsWriteToFile(capturePath + "AdminLanguagesExceptions.txt", SiteType.Equals("Qualifier"));

                        //Continue the test
                        ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
                    }
                    ieInstance.WaitForComplete();
                    System.Threading.Thread.Sleep(1500);
                    languagePage.GetCheckBoxForLanguage(EnglishLocaleName, "Enabled").Checked = true;
                    System.Threading.Thread.Sleep(1000);
                    Span flagSpan = languagePage.GetLanguageFlagSpanByLocaleName(LocaleName);

                    //Assert
                    if (homePage.PageContainsErrors())
                    {
                        //Take a screen Capture
                        ieInstance.CaptureWebPageToFile(CapturePath + "/ErrorOnLanguagesPage.jpg");
                        TestLog.WriteHighlighted("WARNING:");
                        TestLog.WriteLine("An error occurred on the Languages Page, view the corresponding screen shot and exception text log for more information on the error. ");

                        //Record any recent exceptions in the log
                        ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
                        HostSQLPage SQLPage = new HostSQLPage(homePage);
                        SQLPage.CheckForRecentExceptionsWriteToFile(capturePath + "AdminLanguagesExceptions.txt", SiteType.Equals("Qualifier"));

                        //Continue the test
                        ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
                    }
                    watinAssert.AssertIsTrue(flagSpan.Exists, "NoFlagSpan.jpg");
                    watinAssert.AssertIsTrue(ieInstance.Image(Find.ByAlt(LanguageCode)).Exists, "NoLanguageFlag.jpg");
                    watinAssert.AssertIsTrue(flagSpan.ClassName.Equals("Language"), "NewLanguageNotSelected.jpg");
                }
                catch (Exception e)
                {
                    if (!e.ToString().Contains("AssertionFailureException"))
                    {
                        ieInstance.CaptureWebPageToFile(capturePath + "Exception.jpg");
                        Assert.IsTrue(false, "An Exception Occured during the test, view the screen shot for more information. " + System.Environment.NewLine + e.ToString());
                    }
                    else
                    {
                        throw e;
                    }
                }
            }
        }

        [Test]
        [CsvData(ResourcePath = "TestData.UpgradeList.csv", HasHeader = true)]
        [CsvData(ResourcePath = "TestData.InstallList.csv", HasHeader = true)]
        public void Add_New_Language_For_The_Site(string version, string type)
        {
            if (type.Equals("UpgradeOnly"))
            {
                TestLog.WriteLine("This test has not been run on an installation site.");
                Assert.IsTrue(true);
            }
            else if (type.Equals("InstallOnly"))
            {
                TestLog.WriteLine("This test has not been run on an upgraded site.");
                Assert.IsTrue(true);
            }
            else
            {
                //Arrange
                string capturePath = CapturePath + @"\LanguageTests\AddNewLanguageForTheSite\";
                WatiNAssert watinAssert = new WatiNAssert(ieInstance, capturePath);
                if (!Directory.Exists(capturePath))
                {
                    Directory.CreateDirectory(capturePath);
                }
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                LoginPage loginPage = new LoginPage(homePage);
                try
                {
                    loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                    //Assert.IsFalse(homePage.PageContainsErrors());
                    ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
                     if (homePage.PageContainsErrors())
                    {
                        //Take a screen Capture
                        ieInstance.CaptureWebPageToFile(capturePath + "/ErrorOnLanguagesPage.jpg");
                        TestLog.WriteHighlighted("WARNING:");
                        TestLog.WriteLine("An error occurred on the Languages Page, view the corresponding screen shot and exception text log for more information on the error. ");

                        //Record any recent exceptions in the log
                        ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
                        HostSQLPage SQLPage = new HostSQLPage(homePage);
                        SQLPage.CheckForRecentExceptionsWriteToFile(capturePath + "AdminLanguagesExceptions.txt", SiteType.Equals("Qualifier"));

                        //Continue the test
                        ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
                    }
                    LanguagePage languagePage = new LanguagePage(homePage);

                    //Act
                    languagePage.AddNewLanguage(LocaleName, "English (United States)");
                    Span flagSpan = languagePage.GetLanguageFlagSpanByLocaleName(LocaleName);
                    System.Threading.Thread.Sleep(1500);

                    //Assert);
                    if (homePage.PageContainsErrors())
                    {
                        //Take a screen Capture
                        ieInstance.CaptureWebPageToFile(capturePath + "/ErrorOnLanguagesPage.jpg");
                        TestLog.WriteHighlighted("WARNING:");
                        TestLog.WriteLine("An error occurred on the Languages Page, view the corresponding screen shot and exception text log for more information on the error. ");

                        //Record any recent exceptions in the log
                        ieInstance.GoTo(SiteURL + "/Host/SQL.aspx");
                        HostSQLPage SQLPage = new HostSQLPage(homePage);
                        SQLPage.CheckForRecentExceptionsWriteToFile(capturePath + "AdminLanguagesExceptions.txt", SiteType.Equals("Qualifier"));

                        //Continue the test
                        ieInstance.GoTo(SiteURL + "/Admin/Languages.aspx");
                    }
                    watinAssert.AssertIsTrue(flagSpan.Exists, "NoFlagSpan.jpg");
                    watinAssert.AssertIsTrue(ieInstance.Image(Find.ByAlt(LanguageCode)).Exists, "NoLanguageFlag.jpg");
                    watinAssert.AssertIsTrue(flagSpan.ClassName.Equals("Language"), "NewLanguageNotSelected.jpg");
                }
                catch (Exception e)
                {
                    if (!e.ToString().Contains("AssertionFailureException"))
                    {
                        ieInstance.CaptureWebPageToFile(capturePath + "Exception.jpg");
                        string exceptionString = "An Exception Occured during the test, view the screen shot for more information. " + System.Environment.NewLine + @e.ToString();
                        Assert.IsTrue(false, exceptionString);
                    }
                    else
                    {
                        throw e;
                    }
                }
            }
        }

        #endregion
    }
}

